using System; 
using System.Text; 
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration; 
using System.Xml; 
using System.Xml.Serialization;
using SubSonic; 
using SubSonic.Utilities;
namespace Commerce.Common
{
	/// <summary>
	/// Strongly-typed collection for the AttributeItem class.
	/// </summary>
    [Serializable]
	public partial class AttributeItemCollection : ActiveList<AttributeItem, AttributeItemCollection>
	{	   
		public AttributeItemCollection() {}

        /// JASON: From http://stackoverflow.com/questions/1036376/subsonic-filter-in-memory-filter
        /// <summary>
        /// Filters an existing collection based on the set criteria. This is an in-memory filter.
        /// All existing wheres are retained.
        /// </summary>
        /// <returns>NavCollection</returns>
        public AttributeItemCollection Filter(SubSonic.Where w)
        {
            return Filter(w, false);
        }

        /// <summary>
        /// Filters an existing collection based on the set criteria. This is an in-memory filter.
        /// Existing wheres can be cleared if not needed.
        /// </summary>
        /// <returns>NavCollection</returns>
        public AttributeItemCollection Filter(SubSonic.Where w, bool clearWheres)
        {
            if (clearWheres)
            {
                this.wheres.Clear();
            }
            this.wheres.Add(w);
            return Filter();
        }

        /// <summary>
		/// Filters an existing collection based on the set criteria. This is an in-memory filter
		/// Thanks to developingchris for this!
        /// </summary>
        /// <returns>AttributeItemCollection</returns>
		public AttributeItemCollection Filter()
        {
            for (int i = this.Count - 1; i > -1; i--)
            {
                AttributeItem o = this[i];
                foreach (SubSonic.Where w in this.wheres)
                {
                    bool remove = false;
                    System.Reflection.PropertyInfo pi = o.GetType().GetProperty(w.ColumnName);
                    if (pi.CanRead)
                    {
                        object val = pi.GetValue(o, null);
                        switch (w.Comparison)
                        {
                            case SubSonic.Comparison.Equals:
                                if (!val.Equals(w.ParameterValue))
                                {
                                    remove = true;
                                }
                                break;
                        }
                    }
                    if (remove)
                    {
                        this.Remove(o);
                        break;
                    }
                }
            }
            return this;
        }
		
		
	}
	/// <summary>
	/// This is an ActiveRecord class which wraps the CSK_Store_AttributeItem table.
	/// </summary>
	[Serializable]
	public partial class AttributeItem : ActiveRecord<AttributeItem>, IActiveRecord
	{
		#region .ctors and Default Settings
		
		public AttributeItem()
		{
		  SetSQLProps();
		  InitSetDefaults();
		  MarkNew();
		}
		
		private void InitSetDefaults() { SetDefaults(); }
		
		public AttributeItem(bool useDatabaseDefaults)
		{
			SetSQLProps();
			if(useDatabaseDefaults)
				ForceDefaults();
			MarkNew();
		}
        
		public AttributeItem(object keyID)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByKey(keyID);
		}
		 
		public AttributeItem(string columnName, object columnValue)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByParam(columnName,columnValue);
		}
		
		protected static void SetSQLProps() { GetTableSchema(); }
		
		#endregion
		
		#region Schema and Query Accessor	
		public static Query CreateQuery() { return new Query(Schema); }
		public static TableSchema.Table Schema
		{
			get
			{
				if (BaseSchema == null)
					SetSQLProps();
				return BaseSchema;
			}
		}
		
		private static void GetTableSchema() 
		{
			if(!IsSchemaInitialized)
			{
				//Schema declaration
				TableSchema.Table schema = new TableSchema.Table("CSK_Store_AttributeItem", TableType.Table, DataService.GetInstance("dashCommerce"));
				schema.Columns = new TableSchema.TableColumnCollection();
				schema.SchemaName = @"dbo";
				//columns
				
				TableSchema.TableColumn colvarAttributeItemId = new TableSchema.TableColumn(schema);
				colvarAttributeItemId.ColumnName = "AttributeItemId";
				colvarAttributeItemId.DataType = DbType.Int32;
				colvarAttributeItemId.MaxLength = 0;
				colvarAttributeItemId.AutoIncrement = true;
				colvarAttributeItemId.IsNullable = false;
				colvarAttributeItemId.IsPrimaryKey = true;
				colvarAttributeItemId.IsForeignKey = false;
				colvarAttributeItemId.IsReadOnly = false;
				colvarAttributeItemId.DefaultSetting = @"";
				colvarAttributeItemId.ForeignKeyTableName = "";
				schema.Columns.Add(colvarAttributeItemId);
				
				TableSchema.TableColumn colvarTemplateId = new TableSchema.TableColumn(schema);
				colvarTemplateId.ColumnName = "templateId";
				colvarTemplateId.DataType = DbType.Int32;
				colvarTemplateId.MaxLength = 0;
				colvarTemplateId.AutoIncrement = false;
				colvarTemplateId.IsNullable = false;
				colvarTemplateId.IsPrimaryKey = false;
				colvarTemplateId.IsForeignKey = true;
				colvarTemplateId.IsReadOnly = false;
				colvarTemplateId.DefaultSetting = @"";
				
					colvarTemplateId.ForeignKeyTableName = "CSK_Store_AttributeTemplate";
				schema.Columns.Add(colvarTemplateId);
				
				TableSchema.TableColumn colvarName = new TableSchema.TableColumn(schema);
				colvarName.ColumnName = "Name";
				colvarName.DataType = DbType.String;
				colvarName.MaxLength = 50;
				colvarName.AutoIncrement = false;
				colvarName.IsNullable = false;
				colvarName.IsPrimaryKey = false;
				colvarName.IsForeignKey = false;
				colvarName.IsReadOnly = false;
				colvarName.DefaultSetting = @"";
				colvarName.ForeignKeyTableName = "";
				schema.Columns.Add(colvarName);
				
				TableSchema.TableColumn colvarAdjustment = new TableSchema.TableColumn(schema);
				colvarAdjustment.ColumnName = "Adjustment";
				colvarAdjustment.DataType = DbType.Currency;
				colvarAdjustment.MaxLength = 0;
				colvarAdjustment.AutoIncrement = false;
				colvarAdjustment.IsNullable = false;
				colvarAdjustment.IsPrimaryKey = false;
				colvarAdjustment.IsForeignKey = false;
				colvarAdjustment.IsReadOnly = false;
				colvarAdjustment.DefaultSetting = @"";
				colvarAdjustment.ForeignKeyTableName = "";
				schema.Columns.Add(colvarAdjustment);
				
				TableSchema.TableColumn colvarSortOrder = new TableSchema.TableColumn(schema);
				colvarSortOrder.ColumnName = "SortOrder";
				colvarSortOrder.DataType = DbType.Int32;
				colvarSortOrder.MaxLength = 0;
				colvarSortOrder.AutoIncrement = false;
				colvarSortOrder.IsNullable = false;
				colvarSortOrder.IsPrimaryKey = false;
				colvarSortOrder.IsForeignKey = false;
				colvarSortOrder.IsReadOnly = false;
				colvarSortOrder.DefaultSetting = @"";
				colvarSortOrder.ForeignKeyTableName = "";
				schema.Columns.Add(colvarSortOrder);
				
				TableSchema.TableColumn colvarSkuSuffix = new TableSchema.TableColumn(schema);
				colvarSkuSuffix.ColumnName = "SkuSuffix";
				colvarSkuSuffix.DataType = DbType.String;
				colvarSkuSuffix.MaxLength = 50;
				colvarSkuSuffix.AutoIncrement = false;
				colvarSkuSuffix.IsNullable = true;
				colvarSkuSuffix.IsPrimaryKey = false;
				colvarSkuSuffix.IsForeignKey = false;
				colvarSkuSuffix.IsReadOnly = false;
				colvarSkuSuffix.DefaultSetting = @"";
				colvarSkuSuffix.ForeignKeyTableName = "";
				schema.Columns.Add(colvarSkuSuffix);
				
				TableSchema.TableColumn colvarCreatedOn = new TableSchema.TableColumn(schema);
				colvarCreatedOn.ColumnName = "createdOn";
				colvarCreatedOn.DataType = DbType.DateTime;
				colvarCreatedOn.MaxLength = 0;
				colvarCreatedOn.AutoIncrement = false;
				colvarCreatedOn.IsNullable = true;
				colvarCreatedOn.IsPrimaryKey = false;
				colvarCreatedOn.IsForeignKey = false;
				colvarCreatedOn.IsReadOnly = false;
				
						colvarCreatedOn.DefaultSetting = @"(getdate())";
				colvarCreatedOn.ForeignKeyTableName = "";
				schema.Columns.Add(colvarCreatedOn);
				
				TableSchema.TableColumn colvarCreatedBy = new TableSchema.TableColumn(schema);
				colvarCreatedBy.ColumnName = "createdBy";
				colvarCreatedBy.DataType = DbType.String;
				colvarCreatedBy.MaxLength = 50;
				colvarCreatedBy.AutoIncrement = false;
				colvarCreatedBy.IsNullable = true;
				colvarCreatedBy.IsPrimaryKey = false;
				colvarCreatedBy.IsForeignKey = false;
				colvarCreatedBy.IsReadOnly = false;
				colvarCreatedBy.DefaultSetting = @"";
				colvarCreatedBy.ForeignKeyTableName = "";
				schema.Columns.Add(colvarCreatedBy);
				
				TableSchema.TableColumn colvarModifiedOn = new TableSchema.TableColumn(schema);
				colvarModifiedOn.ColumnName = "modifiedOn";
				colvarModifiedOn.DataType = DbType.DateTime;
				colvarModifiedOn.MaxLength = 0;
				colvarModifiedOn.AutoIncrement = false;
				colvarModifiedOn.IsNullable = true;
				colvarModifiedOn.IsPrimaryKey = false;
				colvarModifiedOn.IsForeignKey = false;
				colvarModifiedOn.IsReadOnly = false;
				
						colvarModifiedOn.DefaultSetting = @"(getdate())";
				colvarModifiedOn.ForeignKeyTableName = "";
				schema.Columns.Add(colvarModifiedOn);
				
				TableSchema.TableColumn colvarModifiedBy = new TableSchema.TableColumn(schema);
				colvarModifiedBy.ColumnName = "modifiedBy";
				colvarModifiedBy.DataType = DbType.String;
				colvarModifiedBy.MaxLength = 50;
				colvarModifiedBy.AutoIncrement = false;
				colvarModifiedBy.IsNullable = true;
				colvarModifiedBy.IsPrimaryKey = false;
				colvarModifiedBy.IsForeignKey = false;
				colvarModifiedBy.IsReadOnly = false;
				colvarModifiedBy.DefaultSetting = @"";
				colvarModifiedBy.ForeignKeyTableName = "";
				schema.Columns.Add(colvarModifiedBy);
				
				BaseSchema = schema;
				//add this schema to the provider
				//so we can query it later
				DataService.Providers["dashCommerce"].AddSchema("CSK_Store_AttributeItem",schema);
			}
		}
		#endregion
		
		#region Props
		  
		[XmlAttribute("AttributeItemId")]
		[Bindable(true)]
		public int AttributeItemId 
		{
			get { return GetColumnValue<int>(Columns.AttributeItemId); }
			set { SetColumnValue(Columns.AttributeItemId, value); }
		}
		  
		[XmlAttribute("TemplateId")]
		[Bindable(true)]
		public int TemplateId 
		{
			get { return GetColumnValue<int>(Columns.TemplateId); }
			set { SetColumnValue(Columns.TemplateId, value); }
		}
		  
		[XmlAttribute("Name")]
		[Bindable(true)]
		public string Name 
		{
			get { return GetColumnValue<string>(Columns.Name); }
			set { SetColumnValue(Columns.Name, value); }
		}
		  
		[XmlAttribute("Adjustment")]
		[Bindable(true)]
		public decimal Adjustment 
		{
			get { return GetColumnValue<decimal>(Columns.Adjustment); }
			set { SetColumnValue(Columns.Adjustment, value); }
		}
		  
		[XmlAttribute("SortOrder")]
		[Bindable(true)]
		public int SortOrder 
		{
			get { return GetColumnValue<int>(Columns.SortOrder); }
			set { SetColumnValue(Columns.SortOrder, value); }
		}
		  
		[XmlAttribute("SkuSuffix")]
		[Bindable(true)]
		public string SkuSuffix 
		{
			get { return GetColumnValue<string>(Columns.SkuSuffix); }
			set { SetColumnValue(Columns.SkuSuffix, value); }
		}
		  
		[XmlAttribute("CreatedOn")]
		[Bindable(true)]
		public DateTime? CreatedOn 
		{
			get { return GetColumnValue<DateTime?>(Columns.CreatedOn); }
			set { SetColumnValue(Columns.CreatedOn, value); }
		}
		  
		[XmlAttribute("CreatedBy")]
		[Bindable(true)]
		public string CreatedBy 
		{
			get { return GetColumnValue<string>(Columns.CreatedBy); }
			set { SetColumnValue(Columns.CreatedBy, value); }
		}
		  
		[XmlAttribute("ModifiedOn")]
		[Bindable(true)]
		public DateTime? ModifiedOn 
		{
			get { return GetColumnValue<DateTime?>(Columns.ModifiedOn); }
			set { SetColumnValue(Columns.ModifiedOn, value); }
		}
		  
		[XmlAttribute("ModifiedBy")]
		[Bindable(true)]
		public string ModifiedBy 
		{
			get { return GetColumnValue<string>(Columns.ModifiedBy); }
			set { SetColumnValue(Columns.ModifiedBy, value); }
		}
		
		#endregion
		
		
			
		
		#region ForeignKey Properties
		
		/// <summary>
		/// Returns a AttributeTemplate ActiveRecord object related to this AttributeItem
		/// 
		/// </summary>
		public Commerce.Common.AttributeTemplate AttributeTemplate
		{
			get { return Commerce.Common.AttributeTemplate.FetchByID(this.TemplateId); }
			set { SetColumnValue("templateId", value.TemplateID); }
		}
		
		
		#endregion
		
		
		
		//no ManyToMany tables defined (0)
		
        
        
		#region ObjectDataSource support
		
		
		/// <summary>
		/// Inserts a record, can be used with the Object Data Source
		/// </summary>
		public static void Insert(int varTemplateId,string varName,decimal varAdjustment,int varSortOrder,string varSkuSuffix,DateTime? varCreatedOn,string varCreatedBy,DateTime? varModifiedOn,string varModifiedBy)
		{
			AttributeItem item = new AttributeItem();
			
			item.TemplateId = varTemplateId;
			
			item.Name = varName;
			
			item.Adjustment = varAdjustment;
			
			item.SortOrder = varSortOrder;
			
			item.SkuSuffix = varSkuSuffix;
			
			item.CreatedOn = varCreatedOn;
			
			item.CreatedBy = varCreatedBy;
			
			item.ModifiedOn = varModifiedOn;
			
			item.ModifiedBy = varModifiedBy;
			
		
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}
		
		/// <summary>
		/// Updates a record, can be used with the Object Data Source
		/// </summary>
		public static void Update(int varAttributeItemId,int varTemplateId,string varName,decimal varAdjustment,int varSortOrder,string varSkuSuffix,DateTime? varCreatedOn,string varCreatedBy,DateTime? varModifiedOn,string varModifiedBy)
		{
			AttributeItem item = new AttributeItem();
			
				item.AttributeItemId = varAttributeItemId;
			
				item.TemplateId = varTemplateId;
			
				item.Name = varName;
			
				item.Adjustment = varAdjustment;
			
				item.SortOrder = varSortOrder;
			
				item.SkuSuffix = varSkuSuffix;
			
				item.CreatedOn = varCreatedOn;
			
				item.CreatedBy = varCreatedBy;
			
				item.ModifiedOn = varModifiedOn;
			
				item.ModifiedBy = varModifiedBy;
			
			item.IsNew = false;
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}
		#endregion
        
        
        
        #region Typed Columns
        
        
        public static TableSchema.TableColumn AttributeItemIdColumn
        {
            get { return Schema.Columns[0]; }
        }
        
        
        
        public static TableSchema.TableColumn TemplateIdColumn
        {
            get { return Schema.Columns[1]; }
        }
        
        
        
        public static TableSchema.TableColumn NameColumn
        {
            get { return Schema.Columns[2]; }
        }
        
        
        
        public static TableSchema.TableColumn AdjustmentColumn
        {
            get { return Schema.Columns[3]; }
        }
        
        
        
        public static TableSchema.TableColumn SortOrderColumn
        {
            get { return Schema.Columns[4]; }
        }
        
        
        
        public static TableSchema.TableColumn SkuSuffixColumn
        {
            get { return Schema.Columns[5]; }
        }
        
        
        
        public static TableSchema.TableColumn CreatedOnColumn
        {
            get { return Schema.Columns[6]; }
        }
        
        
        
        public static TableSchema.TableColumn CreatedByColumn
        {
            get { return Schema.Columns[7]; }
        }
        
        
        
        public static TableSchema.TableColumn ModifiedOnColumn
        {
            get { return Schema.Columns[8]; }
        }
        
        
        
        public static TableSchema.TableColumn ModifiedByColumn
        {
            get { return Schema.Columns[9]; }
        }
        
        
        
        #endregion
		#region Columns Struct
		public struct Columns
		{
			 public static string AttributeItemId = @"AttributeItemId";
			 public static string TemplateId = @"templateId";
			 public static string Name = @"Name";
			 public static string Adjustment = @"Adjustment";
			 public static string SortOrder = @"SortOrder";
			 public static string SkuSuffix = @"SkuSuffix";
			 public static string CreatedOn = @"createdOn";
			 public static string CreatedBy = @"createdBy";
			 public static string ModifiedOn = @"modifiedOn";
			 public static string ModifiedBy = @"modifiedBy";
						
		}
		#endregion
		
		#region Update PK Collections
		
        #endregion
    
        #region Deep Save
		
        #endregion
	}
}
